package com.hongyu.smart.village.ui.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.hongyu.smart.village.R;
import com.hongyu.smart.village.config.Constant;
import com.hongyu.smart.village.event.Event;
import com.hongyu.smart.village.ui.adapter.BlueToothDeviceAdapter;
import com.hongyu.smart.village.ui.base.BaseActivity;

import java.util.Set;

public class BluetoothSettingActivity extends BaseActivity {
    @BindView(R.id.sw_bluetooth)
    Switch aSwitch;
    @BindView(R.id.recycle_view_device)
    RecyclerView deviceRecycleView;
    @BindView(R.id.recycle_view_device_can_use)
    RecyclerView canUseRecycleView;
    Set<BluetoothDevice> deviceSet;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initActivityData() {

    }

    @Override
    protected void initContentView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED)
            {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},0x123);
            }else{

            }
        }

        blueToothDeviceAdapter=new BlueToothDeviceAdapter(this,listdata);
        deviceRecycleView.setLayoutManager(new LinearLayoutManager(this));
        deviceRecycleView.setAdapter(blueToothDeviceAdapter);
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    aSwitch.setText("开启");
                    openBlueTooth();

                }else {
                    aSwitch.setText("关闭");
                }
            }
        });


    }

    @SuppressLint("MissingPermission")
    private void openBlueTooth() {


        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADVERTISE) != PackageManager.PERMISSION_GRANTED) {
//            requestBlueToothPermission();
//            return;
//        }
        startActivityForResult(intent, Constant.OPEN_BLUETOOTH_REQUEST_CODE);



    }

    @Override
    protected int getContentView() {
        return R.layout.activity_bluetooth_setting;
    }

    @Override
    protected void onEventProcess(Event event) {

    }
    @OnClick({R.id.tv_scan})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_scan:
                startDiscover();
                break;
        }
    }

    private void startDiscover() {
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent intent){//接受由Intent传递过来的弹窗选择结果
        super.onActivityResult(requestCode,resultCode,intent);
        if(requestCode == Constant.OPEN_BLUETOOTH_REQUEST_CODE){//来自允许蓝牙扫描的对话框
            if(resultCode == RESULT_OK){
                Toast.makeText(this,"允许本地蓝牙被其他设备发现",Toast.LENGTH_SHORT).show();

            }
            else if(resultCode == RESULT_CANCELED){
                Toast.makeText(this,"不允许本地设备被其他蓝牙发现",Toast.LENGTH_SHORT).show();
            }

        }
        if (requestCode == 0x123) {

        }

    }
}